package com.chy.utilsdemo;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.google.common.collect.Lists.newArrayList;


/**
 * @author chy
 * @version 1.0
 * @description Swagger2.0  Configuration       swagger2的配置文件，在项目的启动类的同级文件建立
 * @since 2021/2/1 17:10
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * 配置content type
     */
    private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
            new HashSet<String>(newArrayList("application/json"));

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     */
    private static ApiInfo defaultApiInfo() {
        return new ApiInfoBuilder()
                // 页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTFUL API")
                // 描述
                .description("API 描述")
                // 版本号
                .version("V1.0").build();
    }

    @Bean
    public Docket serviceApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .produces(DEFAULT_PRODUCES_AND_CONSUMES)
                .apiInfo(defaultApiInfo()).select()
                // 使用注解方式
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                // 使用注解方式
                .apis(RequestHandlerSelectors.basePackage("com.chy.utilsdemo.FileUtils"))
                .paths(PathSelectors.any()).build()
                .groupName("文件上传中心")
                .securitySchemes(securityScheme())
                .securityContexts(securityContexts());
    }

    @Bean
    public Docket RedisserviceApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .produces(DEFAULT_PRODUCES_AND_CONSUMES)
                .apiInfo(defaultApiInfo()).select()
                // 使用注解方式
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                // 使用注解方式
                .apis(RequestHandlerSelectors.basePackage("com.chy.utilsdemo.RedisUtils"))
                .paths(PathSelectors.any()).build()
                .groupName("redis服务中心")
                .securitySchemes(securityScheme())
                .securityContexts(securityContexts());
    }

    @Bean
    public Docket ValiddServiceApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .produces(DEFAULT_PRODUCES_AND_CONSUMES)
                .apiInfo(defaultApiInfo()).select()
                // 使用注解方式
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                // 使用注解方式
                .apis(RequestHandlerSelectors.basePackage("com.chy.utilsdemo.validator"))
                .paths(PathSelectors.any()).build()
                .groupName("validator服务中心")
                .securitySchemes(securityScheme())
                .securityContexts(securityContexts());
    }


    private List<SecurityScheme> securityScheme() {
        return newArrayList(new ApiKey(Constants.LOGIN_USER_KEY, Constants.LOGIN_USER_KEY, "header"),
                new ApiKey(Constants.LOGIN_USE_NAME, Constants.LOGIN_USE_NAME, "header"),
                new ApiKey(Constants.LOGIN_USER_TYPE, Constants.LOGIN_USER_TYPE, "header"),
                new ApiKey(Constants.LOGIN_ORG_KEY, Constants.LOGIN_ORG_KEY, "header"),
                new ApiKey(Constants.LOGIN_ORG_NAME_KEY, Constants.LOGIN_ORG_NAME_KEY, "header"),
                new ApiKey("Access-Session-JWT", "Access-Session-JWT", "header"),
                new ApiKey("ZC-Access-Key", "ZC-Access-Key", "header"));
    }

    /**
     * 授权信息全局上下文
     *
     * @return
     */
    private List<SecurityContext> securityContexts() {
        AuthorizationScope[] globalScopes = new AuthorizationScope[]{new AuthorizationScope("global", "全局上下文")};
        return Collections.singletonList(SecurityContext.builder()
                .securityReferences(
                        Lists.newArrayList(
                                new SecurityReference(Constants.LOGIN_USER_KEY, globalScopes),
                                new SecurityReference(Constants.LOGIN_USE_NAME, globalScopes),
                                new SecurityReference(Constants.LOGIN_ORG_KEY, globalScopes),
                                new SecurityReference(Constants.LOGIN_ORG_NAME_KEY, globalScopes),
                                new SecurityReference(Constants.LOGIN_USER_TYPE, globalScopes),
                                new SecurityReference("Access-Session-JWT", globalScopes),
                                new SecurityReference("ZC-Access-Key", globalScopes))
                ).build());
    }

}
